﻿using System;
using System.Windows.Forms;
using System.Xml;
using Core;
using ClassLibrary;
namespace TestProject
{
    public partial class NewChromosomeTable : Form
    {
        private Model model;
        public NewChromosomeTable(Model m)
        {
            InitializeComponent();
            model = m;
        }
        private void create_Click(object sender, EventArgs e)
        {            
            int amount = Convert.ToInt32(pairs_box.Text);
            string path = "file_input\\" + model.model_name + "\\Tables\\ChromosomeTables\\" + name_box.Text + ".xml";
            if (System.IO.File.Exists(path))
            {
                MessageBox.Show("A chromosome table with this name already exists. Please, choose another name.");
                return;
            }
            NewChromGeneLink new_link = new NewChromGeneLink(model, path,name_box.Text,amount);
            if (new_link.ShowDialog(this) != DialogResult.OK)
            {
                MessageBox.Show("Unable to create chromosome table without a link.");
                return;
            }
            NewCrossLink new_cross_link = new NewCrossLink(model,name_box.Text,path,amount);
            if (new_cross_link.ShowDialog(this) != DialogResult.OK)
            {
                MessageBox.Show("Unable to create chromosome table without a link.");
                return;
            }
            XmlTextWriter writer = new XmlTextWriter(path, null);
            writer.Formatting = Formatting.Indented;
            writer.WriteStartDocument();
            writer.WriteStartElement("chromosome_table");
            writer.WriteAttributeString("Name", name_box.Text);
            writer.WriteAttributeString("ChromosomePairsAmount", pairs_box.Text);
            writer.WriteStartElement("chromosomes");
            for (int i = 0; i < amount; i++)
            {
                writer.WriteStartElement("chromosome");
                writer.WriteAttributeString("Id", i.ToString());
                writer.WriteAttributeString("Name", "default"+i.ToString());
                writer.WriteAttributeString("LocusesAmount", "0");
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(System.IO.File.ReadAllText(model.model_path));
            XmlElement el;
            el = doc.CreateElement("table");
            el.SetAttribute("Path", path);
            doc.GetElementsByTagName("tables")[0].Attributes[1].Value = (Convert.ToInt32(doc.GetElementsByTagName("tables")[0].Attributes[1].Value) + 1).ToString();
            doc.DocumentElement.SetAttribute("AmountTables", (Convert.ToInt32(doc.DocumentElement.GetAttribute("AmountTables")) + 1).ToString());
            doc.GetElementsByTagName("chromosome_tables")[0].AppendChild(el);
            doc.Save(model.model_path);
            Functions.add_node_func(new ChromosomeTable(path), name_box.Text, "chromt");
            this.Close();
        }
        private void canc_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
